package com.yanqu.road.server.cmd.activity.seacraftsouthsea;

import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.SeacraftSouthSeaCommonMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2022/1/20 15:28
 */
@Cmd(code = GameProtocol.S_SEACRAFT_SOUTH_SEA_DONATION_SHIP, desc = "海上争霸 南海丝路 捐献")
public class SeacraftSouthSeaDonationShipCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftSouthSeaProto.SeacraftSouthSeaDonationShipReqMsg reqMsg = SeacraftSouthSeaProto.SeacraftSouthSeaDonationShipReqMsg.parseFrom(packet.getBytes());
        ret = SeacraftSouthSeaMgr.canEntryActivity(player,reqMsg.getActivityId());
        if(ret != 0){
            return;
        }
        // 只能在备战期捐献
        if(!SeacraftSouthSeaCommonMgr.isPrepareTime(SeacraftSouthSeaMgr.getActivityInfo(),SeacraftSouthSeaMgr.getConfig())){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NOT_IN_PRETIME;
            return;
        }
        if(reqMsg.getNum() <= 0){
            ret = GameErrorCode.E_BAG_USE_GOODS_COUNT_LESS_ONE;
            return;
        }
        int index = SeacraftSouthSeaMgr.getConfig().donateList.indexOf(reqMsg.getPropId());
        if(index < 0){
            // 道具id 错误
            ret = GameErrorCode.E_BAG_ITEM_CAN_NO_USE;
            return;
        }

        Property consume = new Property(reqMsg.getPropId(), BigInteger.valueOf(reqMsg.getNum()));
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(consume)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        // 预扣除
        player.getModule(PreReduceModule.class).preReduce(consume);
        SeacraftSouthSeaProto.SeacraftSouthSeaDonationShipGameToCrossMsg.Builder gameToCrossMsg = SeacraftSouthSeaProto.SeacraftSouthSeaDonationShipGameToCrossMsg.newBuilder();
        gameToCrossMsg.setActivityId(reqMsg.getActivityId());
        gameToCrossMsg.setCtsMsg(reqMsg);
        gameToCrossMsg.setUnionId(player.getUserInfo().getUnionUid());
        gameToCrossMsg.setConsume(PropertyHelper.parsePropertyToString(consume));
        gameToCrossMsg.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_SEACRAFT_SOUTH_SEA_DONATION_SHIP, gameToCrossMsg, reqMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            SeacraftSouthSeaProto.SeacraftSouthSeaDonationShipRespMsg.Builder resp = SeacraftSouthSeaProto.SeacraftSouthSeaDonationShipRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_DONATION_SHIP,resp);
        }
    }
}
